import java.awt.Point;

public class Tower implements Buildings {

	private byte buildLevel;
	private long timer = System.currentTimeMillis();;

	public Tower(byte level) {
		setBuildLevel(level);
	}

	// no population growth.
	public float growth(float curRes) {
		return 0;
	}

	public byte getBuildLevel() {
		return buildLevel;
	}

	public void setBuildLevel(byte buildLevel) {
		this.buildLevel = buildLevel;
	}

	// Building defense is high in towers
	public float defend(byte attackMorale, byte ownMorale, byte attackArmory) {
		// some formula to reduce number of residents.
		float reduce = (((float) (attackMorale - ownMorale)) / 5f)
				+ ((float) (attackArmory - (buildLevel * 2.5)) / 4f);
		if (reduce < 0) {
			reduce = Math.abs(1 / (reduce - 1));
		} else {
			reduce++;
		}
		return reduce;
	}

	public boolean isUpgradeable(float res) {
		return (buildLevel < 3) && (res > 20);
	}

	public Tower clone() {
		return new Tower(buildLevel);
	}

	public Tower clone(byte level) {
		return new Tower(level);
	}

	/**
	 * Units in the vicinity can be attacked by tower, if the least minimum time
	 * has expired. If attacked, unit is set to null (and no reference to that unit should
	 * exist) and timer resets.
	 */
	public void InRange(Point pos, Unit u) {
		// distance between tower and point
		if (u.getPosition().distance(pos) < 1000) {
			// least time between attacks, adjustments needed?
			// lvl 0 1000ms
			// lvl 1 666ms
			// lvl 2 500ms
			// lvl 3 400ms
			if (System.currentTimeMillis() - timer > (2000 / (buildLevel + 2))) {
				u = null;
				timer = System.currentTimeMillis();
			}
		}
	}
}
